//
// * Copyright (c) 2002-2009 "Neo Technology,"
// *     Network Engine for Objects in Lund AB [http://neotechnology.com]
// *
// * This file is part of Neo4j.
// * 
// * Neo4j is free software: you can redistribute it and/or modify
// * it under the terms of the GNU Affero General Public License as
// * published by the Free Software Foundation, either version 3 of the
// * License, or (at your option) any later version.
// * 
// * This program is distributed in the hope that it will be useful,
// * but WITHOUT ANY WARRANTY; without even the implied warranty of
// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// * GNU Affero General Public License for more details.
// * 
// * You should have received a copy of the GNU Affero General Public License
// * along with this program. If not, see <http://www.gnu.org/licenses/>.
// 
namespace org.neo4j.kernel.impl.transaction.xaframework
{


	internal class DirectLogBuffer : LogBuffer
	{
		private readonly FileChannel fileChannel;
		private readonly ByteBuffer buffer;

		internal DirectLogBuffer(FileChannel fileChannel, ByteBuffer buffer)
		{
			if (fileChannel == null || buffer == null)
			{
				throw new IllegalArgumentException("Null argument");
			}
			if (buffer.capacity() < 8)
			{
				throw new IllegalArgumentException("Capacity less then 8");
			}
			this.fileChannel = fileChannel;
			this.buffer = buffer;
		}

//JAVA TO VB & C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public LogBuffer put(byte b) throws IOException
		public virtual LogBuffer put(sbyte b)
		{
			buffer.Clear();
			buffer.put(b);
			buffer.flip();
			fileChannel.write(buffer);
			return this;
		}

//JAVA TO VB & C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public LogBuffer putInt(int i) throws IOException
		public virtual LogBuffer putInt(int i)
		{
			buffer.Clear();
			buffer.putInt(i);
			buffer.flip();
			fileChannel.write(buffer);
			return this;
		}

//JAVA TO VB & C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public LogBuffer putLong(long l) throws IOException
		public virtual LogBuffer putLong(long l)
		{
			buffer.Clear();
			buffer.putLong(l);
			buffer.flip();
			fileChannel.write(buffer);
			return this;
		}

//JAVA TO VB & C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public LogBuffer put(byte[] bytes) throws IOException
		public virtual LogBuffer put(sbyte[] bytes)
		{
			fileChannel.write(ByteBuffer.wrap(bytes));
			return this;
		}

//JAVA TO VB & C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public LogBuffer put(char[] chars) throws IOException
		public virtual LogBuffer put(char[] chars)
		{
			int position = 0;
			do
			{
				buffer.Clear();
				int leftToWrite = chars.Length - position;
				if (leftToWrite * 2 < buffer.capacity())
				{
					buffer.asCharBuffer().put(chars, position, leftToWrite);
					buffer.limit(leftToWrite * 2);
					fileChannel.write(buffer);
					position += leftToWrite;
				}
				else
				{
					int length = buffer.capacity() / 2;
					buffer.asCharBuffer().put(chars, position, length);
					buffer.limit(length * 2);
					fileChannel.write(buffer);
					position += length;
				}
			} while (position < chars.Length);
			return this;
		}

//JAVA TO VB & C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public void force() throws IOException
		public virtual void force()
		{
			fileChannel.force(false);
		}

//JAVA TO VB & C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public long getFileChannelPosition() throws IOException
		public virtual long getFileChannelPosition()
		{
			return fileChannel.position();
		}

		public virtual FileChannel getFileChannel()
		{
			return fileChannel;
		}
	}
}